<template>
  <div>
    <!-- 表格 -->
    <el-table :data="records" style="width: 100%" border>
      <el-table-column type="index" label="序号" align="center" width="80" />
      <el-table-column prop="skuName" label="名称" width="width" />
      <el-table-column prop="skuDesc" label="描述" width="width" />
      <el-table-column prop="prop" label="默认图片" width="110">
        <template slot-scope="{row}">
          <img :src="row.skuDefaultImg" style="width:80px;height:80px" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="80" />
      <el-table-column prop="price" label="价格" width="80" />
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{row}">
          <el-button v-if="row.isSale == 1" size="mini" type="success" icon="el-icon-upload2" @click="download(row)" />
          <el-button v-else size="mini" type="success" icon="el-icon-download" @click="upload(row)" />
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="$message.info('正在开发中')" />
          <el-button size="mini" type="info" icon="el-icon-info" @click="showDrawer(row)" />
          <el-button size="mini" type="danger" icon="el-icon-delete" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      style="text-align:center"
      layout=" prev, pager, next, jumper ,->, sizes,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="getSkuList"
    />
    <!-- 抽屉 -->
    <el-drawer
      size="50%"
      :show-close="false"
      :visible.sync="drawer"
    >
      <el-row>
        <el-col :span="6">名称</el-col>
        <el-col :span="15">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="6">描述</el-col>
        <el-col :span="15">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="6">价格</el-col>
        <el-col :span="15">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="6">平台属性</el-col>
        <el-col :span="15">
          <template>
            <el-tag v-for="tag in skuInfo.skuAttrValueList" :key="tag.id" style="margin:0px 10px" type="success">{{ tag.attrId }}-{{ tag.valueId }}</el-tag>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">商品图片</el-col>
        <el-col :span="15">
          <template>
            <el-carousel height="150px" trigger="click">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="">
              </el-carousel-item>
            </el-carousel>
          </template>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'SKU',
  data() {
    return {
      page: 1,
      limit: 10,
      records: [],
      total: 0,
      drawer: false,
      skuInfo: {}
    }
  },
  created() {
    this.getSkuList()
  },
  methods: {
    async showDrawer(row) {
      this.drawer = true
      // 获取SKU数据
      const result = await this.$API.sku.reqSkuById(row.id)
      if (result.code == 200) {
        this.skuInfo = result.data
      }
    },
    // 上架
    async upload(row) {
      try {
        await this.$API.sku.reqSale(row.id)
        this.$message.success('上架成功')
        row.isSale = 1
      } catch (error) {
        console.log(error
        )
      }
    },
    // 下架
    async download(row) {
      try {
        await this.$API.sku.reqCancel(row.id)
        this.$message.success('下架成功')
        row.isSale = 0
      } catch (error) {
        console.log(error
        )
      }
    },
    // 获取数据
    async getSkuList(pages = 1) {
      this.page = pages
      try {
        const res = await this.$API.sku.reqSkuList(this.page, this.limit)
        this.total = res.data.total
        this.records = res.data.records
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange() {},
    handleCurrentChange() {}
  }
}
</script>

<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }

  .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }
</style>

<style scoped>
.el-col-6{
  text-align: right;
}
.el-col{
  margin: 10px 10px;
}
>>>.el-carousel__button{
    width:10px;
    height:10px;
    background:red;
    border-radius:50%;
  }

</style>
